package bstsetcode;



/**
 * 定义一个泛型类
 * @author noob
 * @version 1.0
 * @date 2021/3/13 9:45
 */
public class LinearSearchTest<E> {


    private LinearSearchTest() {
    }

    public static <E> int search(E[] data, E target){

        for (int i = 0; i < data.length; i++) {
            if(data[i].equals(target)){ //注意
                return i;
            }
        }
        return -1;
    }

    /**
     * 测试线性查找的性能
     * @param args
     * 思维：测试一次不太稳定，测试100次，查看结果  规模效应
     */
    public static void main(String[] args) {
//        int n = 100000;
//        int n = 1000000;
        int[] dataSize = {100000,1000000};
        for(int n : dataSize){
            final Integer[] data = ArrayGenerator.generateOrderArray(n);
            long startTime = System.nanoTime();
            for (int i = 0; i < 100; i++) {
                LinearSearchTest.search(data, n);
            }
            long endTime = System.nanoTime();
            double time =   (endTime - startTime) / 1000000000.0;
            System.out.println( "n="+ n + "，100次循环:"+ time +" s");
        }





    }




}
